package com.bamboo.framework.framework.security.controller;


import com.bamboo.framework.framework.redis.util.RedisUtil;
import com.bamboo.framework.framework.security.entity.User;
import com.bamboo.framework.framework.security.service.IUserService;
import com.bamboo.framework.common.format.ReturnMsg;
import com.bamboo.framework.common.enums.Status;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 用户管理层
 * </p>
 *
 * @author GuoQing
 * @since 2018-12-14
 */
@RestController
@RequestMapping("/user")
public class UserController {

  @Resource
  public RedisUtil redisUtil;

  @Resource
  private IUserService iUserService;

  /**
   * 获取用户信息
   *
   * @param bambooToken 登陆标识
   */
  @GetMapping("info")
  public ReturnMsg getInfo(@RequestHeader String bambooToken) {
    return new ReturnMsg(Status.SUCCESS, redisUtil.getObject(bambooToken));
  }

  /**
   * 退出登陆
   *
   * @param bambooToken 登陆标识
   */
  @PostMapping("logout")
  public ReturnMsg logout(@RequestHeader String bambooToken) {
    redisUtil.delete(bambooToken);
    return new ReturnMsg(Status.NOT_LOGGED_IN, "已退出登陆");
  }


  /**
   * 分页获取用户
   */
  @PostMapping("getPage")
  public ReturnMsg getPage(@RequestParam(defaultValue = "10") String size,
      @RequestParam(defaultValue = "1") String current,
      @RequestParam(required = false) String phone, @RequestParam(required = false) String nickname,
      @RequestParam(required = false) String username) {

    return iUserService
        .getPage(Integer.valueOf(current).intValue(), Integer.valueOf(size).intValue(), phone,
            nickname, username);
  }

  /**
   * 保存用户
   * @param user 用户实体
   */
  @PostMapping("saveUser")
  public ReturnMsg saveUser(@RequestBody User user){
    return iUserService.saveUser(user);
  }
}
